package cn.youwan.market.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EncodingUtils;

import android.util.Log;
import cn.youwan.market.utils.Const;
import cn.youwan.market.utils.Utils;

public class StarHttpClient {
	private HttpClient mHttpClient;
	public HttpTransport mHttpTransport;
	private List<NameValuePair> params;
	private String reciveData;
	private String key;
	private boolean isRefresh;

	public void setKey(String key) {
		this.key = key;
	}

	public void setIsRefresh(boolean isrefresh) {
		this.isRefresh = isrefresh;
	}

	public StarHttpClient() {
	}

	public List<NameValuePair> getParams() {
		return params;
	}

	public void addParam(String key, String value) {
		if (this.params == null)
			this.InitCommonParams();
		params.add(new BasicNameValuePair(key, value));
	}

	public void sendPost(String url, boolean proxy) throws Exception {
		if (mHttpClient == null)
			mHttpClient = HttpClientFactory.newInstance(120000);
		if (mHttpTransport == null) {
			mHttpTransport = new HttpTransport(mHttpClient);
		}
		try {
			System.out.println("url : " + url);
			Log.i("downs", url);
			getData(url, proxy);
		} catch (IOException ex) {
			throw ex;
		}
	}

	public void sendGet(String url, boolean proxy) throws Exception {
		if (mHttpClient == null)
			mHttpClient = HttpClientFactory.newInstance(120000);
		if (mHttpTransport == null) {
			mHttpTransport = new HttpTransport(mHttpClient);
		}
		try {
			getGetData(url, proxy);
		} catch (IOException ex) {
			throw ex;
		}
	}

	private void getGetData(String url, boolean proxy)
			throws ClientProtocolException, IOException {
		String message = read(key);
		if (!message.equals("")) {
			this.reciveData = message;
		} else {
			this.reciveData = mHttpTransport.get(proxy, url, params);
			if (key != null && !key.equals(""))
				save(key, this.reciveData);
		}
	}

	private void getData(String url, boolean proxy)
			throws ClientProtocolException, IOException {
		String message = read(key);
		if (!message.equals("")) {
			this.reciveData = message;
		} else {
			this.reciveData = mHttpTransport.post(proxy, url, params);
			Log.v("message", this.reciveData);
			if (key != null && !key.equals(""))
				save(key, this.reciveData);
		}
	}

	private void InitCommonParams() {
		if (params == null)
			params = new ArrayList<NameValuePair>();
	}

	public void setReciveData(String reciveData) {
		this.reciveData = reciveData;
	}

	public String getReciveData() {
		return reciveData;
	}

	public void dispose() {
		this.mHttpClient = null;
		this.mHttpTransport = null;
		this.params = null;
		this.reciveData = null;
	}

	public boolean save(String key, String message) {
		if (!Utils.hasSDCard()) {
			return false;
		}
		try {
			String path = getCachePath(key);
			File file = new File(path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdir();
			}
			FileOutputStream out = new FileOutputStream(file);
			byte[] bytes = message.getBytes();
			out.write(bytes);
			out.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String read(String key) {
		if (isRefresh) {
			return "";
		}
		if (!Utils.hasSDCard()) {
			return "";
		}
		if (key == null || key.equals("")) {
			return "";
		}
		try {
			String path = getCachePath(key);
			File file = new File(path);
			if (!file.exists()) {
				return "";
			}
			FileInputStream in = new FileInputStream(file);
			int length = in.available();
			byte[] buffer = new byte[length];
			in.read(buffer);
			String message = EncodingUtils.getString(buffer, "UTF_8");
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	private String getCachePath(String key) {
		String pirName = Utils.md5(key);
		String filePath = Const.DataCache + pirName;
		return filePath;
	}
}
